from sqlalchemy import Column, String, DateTime, Table, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from datetime import datetime
import uuid

from db.base import Base


class DocumentTag(Base):
    """文档标签模型"""
    __tablename__ = "document_tag"

    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    name = Column(String(50), nullable=False, unique=True, comment="标签名称")
    color = Column(String(20), default="#007bff", comment="标签颜色")
    created_at = Column(DateTime, default=datetime.utcnow(), comment="创建时间")

    # 关系定义
    documents = relationship("PersonalDocument", secondary="document_tag_relation", back_populates="tags")

    def __repr__(self):
        return f"<DocumentTag(id={self.id}, name='{self.name}')>"


# 文档标签关联表
document_tag_relation = Table(
    'document_tag_relation',
    Base.metadata,
    Column('document_id', UUID(as_uuid=True), ForeignKey('personal_document.id', ondelete='CASCADE'), primary_key=True),
    Column('tag_id', UUID(as_uuid=True), ForeignKey('document_tag.id', ondelete='CASCADE'), primary_key=True)
)
